m.controller("publicCtrl",function($rootScope,$scope) {
	

$scope.init = function(){

	$scope.pic = "url";
	$scope.add_name = "";
    $scope.add_price = "";
    $scope.add_picture = "";
    $scope.add_summary = "";
    $scope.add_description = "";
    
}

$scope.init();
var filePicture="";

$scope.addComm = function () {
  
	if($scope.pic == "url"){

	    $scope.add_picture =  $scope.image;
	}else if($scope.pic == "file"){
		$scope.add_picture = $scope.fileName;
	}
	$scope.add_name = $scope.name;
    $scope.add_price = $scope.price;
    $scope.add_summary = $scope.summary;
    $scope.add_description = $scope.description;
	if($scope.add_picture==""||$scope.add_picture== null){
		
		$scope.htmlInit;
	}else{
		
		if($scope.add_price <= 0){
			alert("价格必须大于0");
		}else{
		$.ajax({
        type: "POST",
        url: "/commodity/addComm",
        data: {"name": $scope.add_name ,"price":$scope.add_price,"picture":$scope.add_picture,
            "summary":$scope.add_summary,"description":$scope.add_description},
        contentType: "application/x-www-form-urlencoded",
        dataType: "json",
        success: function (data) {
        	if(data.success){
        		location.href = "/publicSubmit?id="+data.value;
            	
        	}else{
        		alert("错误");
        	}
        }
    });
			
		}

	}
};

$scope.fileChanged = function(obj){
	filePicture = "/resources/images/"+
    	    obj.value.substring(obj.value.lastIndexOf("\\")+1);
  
}
/**
 * 上传图片
 * @type {boolean}
 */
$scope.doUpload = function () {  
	 	var form = new FormData();
		var vfile = $("#uploadfile");
		var vfiles = vfile[0].files[0];
		var maxAllowedSize = 1000000;
		
		if(vfiles.size > maxAllowedSize) {
			alert("超过文件上传大小限制");
		}else{
			form.append('uploadfile', vfiles, vfiles.name);
			form.enctype = "multipart/form-data";
        	 $.ajax({
        	        url: '/commodity/addCommPicture' ,
        	        type: 'POST',
        	        data: form,
                    async: false,  
                    cache: false,  
                    contentType: false,  
                    processData: false,
                    dataType: 'json', //返回值类型，一般设置为json、application/json
        	        success: function (returndata) {
        	            alert("图片上传成功");
        	           	 $scope.fileName = filePicture;
        	        
        	        },
        	        error: function (returndata) {
        	             alert("图片上传失败");
        	        }
        	    });}
            
        }

   
$scope.showPic = function (obj) {

	$scope.$apply(function(){
		$scope.image = obj.value;
	});
}

});

